import Vue from 'vue';
import VueRouter from 'vue-router';
import Layout from '@/views/Layout/Layout.vue';
import Home from '@/views/Home/Home.vue';
import Search from '@/views/Search/Search.vue';
import MusicDetail from '@/views/Item/MusicDetail.vue';
import ItemMusic from '@/views/Play/ItemMusic.vue';
import Friends from '@/views/Friends/Friends.vue';
import Login from '@/views/Login/Login.vue';
import store from '@/store';
Vue.use(VueRouter);

const routes = [

    {
        path: '/',
        redirect: '/layout',

    },
    {
        path: '/layout',
        redirect: '/layout/home',
        component: Layout,

        children: [
            {
                path: 'home',
                component: Home,
                meta: {title: '首页'},
            },
            {
                path: 'search',
                component: Search,
                meta: {title: '搜索'},
            },
            {

                path: 'friends',
                component: Friends,
                meta: {title: '我的'},
                beforeEnter: (to, from, next) => {
                    if (store.state.isLogin || store.state.token ||
                        localStorage.getItem('token')) {
                        next();
                    } else {
                        next('/login');
                    }
                },
            },
        ],
    },
    {
        path: '/musicDetail',
        component: MusicDetail,
    },
    {
        path: '/itemMusic',
        component: ItemMusic,
    },

    {
        path: '/login',
        component: Login,
    },

];

const router =new VueRouter({
    routes
})

export default router